#include <iostream>
#include <pthread.h>
#include <string.h>
#include <unistd.h>
#include "BlockingQueue.hpp"
#include "Task.hpp"

typedef Task dataclass;
BlockQueue<dataclass> q(5);
int _cnt = 1;

void *consumer(void *args)
{
    while (1)
    {
        sleep(2);
        std::string name = static_cast<char *>(args);
        dataclass data = q.popqueue();
        //std::cout << name << "取得任务:" << data << std::endl;
        //data();
        data.Execute();
        std::cout<<data.X()<<" "<<data.Y()<<"="<<data.Result()<<std::endl;
    }
}
void *product(void *args)
{
    while (1)
    {
        std::string name = static_cast<char *>(args);
        q.inqueue(Task(5,2));
        //std::cout << name << "发布任务:" << _cnt++ << std::endl;
        std::cout << name << "发布任务:" << std::endl;
        
    }
}

int main()
{

    pthread_t t1, t2, t3, t4;
    pthread_create(&t1, nullptr, consumer, (void *)"thread 1:");
    pthread_create(&t2, nullptr, product, (void *)"thread 2:");
    pthread_create(&t3, nullptr, product, (void *)"thread 3:");
    pthread_create(&t4, nullptr, product, (void *)"thread 4:");
    pthread_join(t1,nullptr);
    pthread_join(t2,nullptr);
    pthread_join(t3,nullptr);
    pthread_join(t4,nullptr);
    return 0;
}